package com.baidu.common.views;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.japher.hackson.R;

public class LoadingDialog extends Dialog implements android.view.View.OnClickListener {
    private Window window;
    private String message;
    private Context context;
    
    public LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
        init();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupView();
    }
    
    private void init() {
        window = getWindow();
    }
    
    @Override
    public void show() {
        try {
            window.setGravity(Gravity.CENTER_VERTICAL);
            super.show();
        } catch (Exception e) {
        }
    }
    
    public void dispose() {
        try {
            dismiss();
        } catch (Exception e) {
            
        }
    }
    
    private void setupView() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        window.setBackgroundDrawable(context.getResources().getDrawable(
            R.drawable.bg_floating_dialog));
        LinearLayout layout = new LinearLayout(context);
        layout.setPadding(15, 15, 15, 15);
        LinearLayout.LayoutParams param = new LayoutParams(-1, -2);
        layout.setLayoutParams(param);
        
        ProgressBar progressBar = new ProgressBar(context);
        LinearLayout.LayoutParams param2 = new LayoutParams(-2, -2);
        param2.gravity = Gravity.CENTER_VERTICAL;
        layout.addView(progressBar, param2);
        
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams param3 = new LayoutParams(-2, -2);
        param3.gravity = Gravity.CENTER_VERTICAL;
        textView.setLayoutParams(param3);
        textView.setGravity(Gravity.CENTER);
        textView.setText(this.message);
        textView.setTextColor(Color.WHITE);
        textView.setPadding(10, 0, 0, 0);
        layout.addView(textView);
        
        LinearLayout.LayoutParams param4 = new LayoutParams(-1, -1);
        param4.gravity = Gravity.CENTER;
        addContentView(layout, param4);
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    @Override
    public void onClick(View v) {
        
    }
    
}
